package app;

import java.util.*;

public class AttributeSet {
	private Vector<Attribute> attributes;
	private Hashtable<Attribute, Integer> attributeHash;
	
	public AttributeSet() {
		attributes = new Vector<Attribute>();
		attributeHash = new Hashtable<Attribute, Integer>();
	}
	
	public AttributeSet(AttributeSet attributeSet) {
		this();
		for (int i = 0; i < attributeSet.size(); i++) {
			add(attributeSet.get(i));
		}
	}
	
	public AttributeSet(AttributeSet attributeSet, Attribute exceptAttribute) {
		this();
		for (int i = 0; i < attributeSet.size(); i++) {
			Attribute attribute = attributeSet.get(i);
			if (! attribute.equals(exceptAttribute))
				add(attribute);
		}
	}
	
	public void add(Attribute attribute) {
		if(attribute == null)
			throw new IllegalArgumentException("Invalid item");
		
		attributeHash.put(attribute, new Integer(attributes.size()));
		attributes.add(attribute);
	}
	
	public Attribute get(int index) {
		return attributes.get(index);
	}
	
	public int indexOf(Attribute attribute) {
		return attributeHash.get(attribute).intValue();
	}
	
	public Attribute findByName(String name) {
		for (Attribute attribute : attributes) {
			if (attribute.getName().equals(name))
				return attribute;
		}
		
		return null;
	}
	
	public int size() {
		return attributes.size();
	}
	
	public String toString() {
		String str = "Attributes: ";
		
		for (Attribute attribute : attributes) {
			str += attribute + " ";
		}
		
		return str;
	}
}
